
package com.yangl.pluginmaindemo;

import java.util.List;

import dalvik.system.DexClassLoader;
import dalvik.system.PathClassLoader;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;

public class MainActivity extends Activity
{
    public static final String ACTION_PLUGIN = "com.intsig.mainApp.PLUGIN";

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try
        {
            //            查找符合这个action的所有activity即插件，若插件使用的是其他组件换成对应的方法
            List<ResolveInfo> infos = getPackageManager().queryIntentActivities(new Intent(ACTION_PLUGIN), PackageManager.MATCH_DEFAULT_ONLY);
            ActivityInfo pluginInfo;
            for (ResolveInfo info : infos)
            {
                pluginInfo = info.activityInfo;
                
                ClassLoader cl = new PathClassLoader(pluginInfo.applicationInfo.sourceDir, getClassLoader());
                int a = 0;
                a++;
                MyInterface plugin = (MyInterface) cl.loadClass(pluginInfo.name).newInstance();
                plugin.test();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

//    private void test(){
////        Intent intent = new Intent(com.example.plugindevandroid.plugin,null);
////        final PackageManager pm = getPackageManager();
////        final List plugins = pm.queryIntentActivities(intent, 0);
//        List<ResolveInfo> infos = getPackageManager().queryIntentActivities(new Intent(ACTION_PLUGIN), PackageManager.MATCH_DEFAULT_ONLY);
//        ActivityInfo pluginInfo;
//        for(ResolveInfo r:infos){
//            ActivityInfo activityInfo = r.activityInfo;
//            
//            String div = System.getProperty(path.separator);
//            String packageName = activityInfo.packageName;
//            String packageName0 = getPackageName();
//            String dexPath = activityInfo.applicationInfo.sourceDir;
//            String dexOutputDir1 = activityInfo.applicationInfo.dataDir;
//            String dexOutputDir2 = getApplicationInfo().dataDir;
//            String libPath = activityInfo.applicationInfo.nativeLibraryDir;
//            
////            Log.d(hlwang, MainActivity test div is:+div
////                    +,packageName is:+packageName
////                    +,packageName0 is:+packageName0
////                    +,dexPath is:+dexPath
////                    +,dexOutputDir1 is:+dexOutputDir1
////                    +,dexOutputDir2 is:+dexOutputDir2
////                    +,libPath is:+libPath);
//            
//            DexClassLoader dexCl = new DexClassLoader(dexPath, dexOutputDir2, libPath, getClassLoader());
////            Log.d(hlwang, MainActivity test clazzName is:+packageName+.PluginVersion);
//            try{
//                Class clazz = dexCl.loadClass(packageName+.PluginVersion);
//                IPluginDev plugin = (IPluginDev) clazz.newInstance();
//                String name = plugin.getName();
//                Log.d(hlwang, MainActivity test name is:+name);
//                
//                Resources res = pm.getResourcesForApplication(packageName);
//                int id = 0;
//                id = res.getIdentifier(version, string, packageName);
//                String version = res.getString(id);
//                Log.d(hlwang, MainActivity test version is:+version);
//                
//                
//                PluginObject p = new PluginObject();
//                p.name = name;
//                p.version = version;
//                Log.d(hlwang, MainActivity test p is:+p);
//                mList.add(p);
//            }catch(Exception e){
//                Log.d(hlwang, MainActivity exception eeeeeeeeeeeee);
//                e.printStackTrace();
//            }
//            
//        }
////        Log.d(hlwang, MainActivity test list size is:+mList.size());
////        setListAdapter(new ArrayAdapter(this,
////                android.R.layout.simple_list_item_1, mList));
//    }
}
